//
//  MainViewController.swift
//  Sinnaweibo模拟项目
//
//  Created by 尹家鑫 on 16/3/6.
//  Copyright © 2016年 尹家鑫. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {
    
    //跳转页面
    @objc private func plusBtnDidClick() {
        print(__FUNCTION__)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        let mainTabBar = MainTabBar()
        
        //添加加号的点击事件
        mainTabBar.plusBtn.addTarget(self, action: "plusBtnDidClick", forControlEvents: .TouchUpInside)
        
        //KVC 给对象提供类似字典的方式，访问对象的属性
        setValue(mainTabBar, forKey: "TabBar")
        
        //添加所有子视图控制器
        addChildViewControllers()
        
    }
    
    private func addChildViewControllers() {
        addChildViewController(HomeViewController(), title: "首页", imageNamed: "tabbar_home")
        addChildViewController(MessageViewController(), title: "消息", imageNamed: "tabbar_message_center")
        addChildViewController(DiscoverViewController(), title: "发现", imageNamed: "tabbar_discover")
        addChildViewController(ProfileViewController(), title: "我", imageNamed: "tabbar_profile")
    }
    
    //MARK：添加子试图控制器
    private func addChildViewController(vc: UIViewController,title: String,imageNamed: String) {
    
        //设置title
        vc.title = title
        vc.tabBarItem.title = title
        
        //设置tintColor
        tabBar.tintColor = UIColor.orangeColor()
        
        //设置图片
        vc.tabBarItem.image = UIImage(named:imageNamed)
        
        //设置选中图片
        vc.tabBarItem.selectedImage = UIImage(named: imageNamed + "_selected")?.imageWithRenderingMode(.AlwaysOriginal)
        
        //控制器跳转
        let nav = UINavigationController(rootViewController: vc)
        
        addChildViewController(nav)
    }

}
